<?php
include_once('../class/Controller.class.php');
include_once('../class/Common.class.php');
include_once('modules/home/models/HomeModel.php');

define('PER_PAGE', 12);
class HomeController extends Controller {

    public function index() {
        $this->listVova();
    }

    public function insert() {
        if (isset($_POST['submit'])) {
            $db = new HomeModel();
            $params = array();
            $params[] = 'sss';
            $params[] = $_POST['title'];
            $params[] = $_POST['content'];
            $params[] = $_POST['character'];

            $result = $db->insertVova($params);
            if ($result == true) {
                $this->data['notice'] = 'success';
                $this->data['act_insert'] = 'Thêm mới thành công !';
            } else {
                $this->data['notice'] = 'error';
                $this->data['act_insert'] = 'Thêm mới không thành công !';
            }
            $this->listVova();
        } else {
            $this->data['title'] = '';
            $this->data['content'] = '';
            $this->data['character'] = '';
            $this->data['act_title'] = 'Thêm mới';
            $this->data['act'] = 'insert';
            $this->data['character'] = 'Vova';
            $this->render( "modules/home/views/HomeView.php", $this->data);
        }
    }

    public function edit() {
        if (isset($_POST['submit']) && $_POST['id'] > 0) {
            $db = new HomeModel();
            $params = array();
            $params[] = 'sssi';
            $params[] = $_POST['title'];
            $params[] = $_POST['content'];
            $params[] = $_POST['character'];
            $params[] = $_POST['id'];

            $result = $db->editVova($params);
            if ($result == true) {
                $this->data['notice'] = 'success';
                $this->data['act_insert'] = 'Thay đổi thành công !';
            } else {
                $this->data['notice'] = 'error';
                $this->data['act_insert'] = 'Thay đổi không thành công !';
            }
            $this->listVova();
        } else {
            $db = new HomeModel();
            $id = $_GET['id'];
            $result = $db->getVovaById($id);
            $this->data['title'] = $result[0]['title'];
            $this->data['content'] = $result[0]['content'];
            $this->data['character'] = $result[0]['character'];
            $this->data['id'] = $result[0]['id'];

            $this->data['act_title'] = 'Thay đổi';
            $this->data['act'] = 'edit';
            $this->render( "modules/home/views/HomeView.php", $this->data);
        }
    }

    public function listVova() {
        $page = (isset($_GET['page']) && $_GET['page'] ) ? (int)$_GET['page'] : 1;
        $start = ($page - 1) * PER_PAGE; //offset
        $this->data['page_curent'] = $page;
        $this->data['per_page'] = PER_PAGE;
        $cm = new Common();

        $db = new HomeModel();
        $strSearch = '';
        if (isset($_POST['search'])) {
            $strSearch = $_POST['search'];
        }elseif (isset($_GET['page'])) {
            $strSearch = $_SESSION['strSearch'];
        }
        $_SESSION['strSearch'] = $strSearch;


        $list = $db->listVova($start, PER_PAGE, $_SESSION['strSearch']);
        $total = $db->countVova($_SESSION['strSearch']);

        $this->data['URL'] = $cm->curPageURL();
        $this->data['total'] = $total;
        $this->data['list'] = $list;
        $this->data['strSearch'] = $_SESSION['strSearch'];

        $this->data['act_title'] = 'Danh sách';
        $this->render( "modules/home/views/ListvovaView.php", $this->data);
    }

    public function delete() {
        $db = new HomeModel();
        $id = $_GET['id'];
        $result = $db->delete($id);
        if ($result == true) {
            $this->data['notice'] = 'success';
            $this->data['act_insert'] = 'Xóa thành công !';
        } else {
            $this->data['notice'] = 'error';
            $this->data['act_insert'] = 'Xóa không thành công !';
        }

        $this->listVova();
    }
}